package com.magicsoft.app.wcf;

import java.lang.reflect.Type;
import java.util.List;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.boyuan.huijiaoyu.R;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.magicsoft.app.entity.AuthorizationHistoryListResp;
import com.magicsoft.app.entity.AuthorizationListResp;
import com.magicsoft.app.entity.DoorInfoResp;
import com.magicsoft.app.helper.TokenHelper;
import com.magicsoft.app.wcf.listener.GetMutilResultListener;
import com.magicsoft.app.wcf.listener.GetOneRecordListener;
import com.magicsoft.app.wcf.listener.GetTwoRecordListener;
import com.magicsoft.constant.Constant;

/**
 * 门禁
 * 
 * @author liqingjun
 * 
 */
public class DoorControlService extends BaseService {

	public final static String TAG = "DoorControlService";

	public DoorControlService(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	/**
	 * 开门
	 * 
	 * @param doorid
	 *            门禁编号
	 * @param listener
	 *            回调函数
	 */
	public void openDoor(String URL, String qrcode,
			final GetTwoRecordListener<String, String> listener) {

		RequestParams params = new RequestParams();
		params.put("qrcode", qrcode);
		Log.i(TAG, "qrcode = " + qrcode);

		String getParamString = "";

		try {//
			getParamString = TokenHelper.FetchToken(context, true);
		} catch (Exception e) {
		}

		String requestUrl = URL + "door/open" + getParamString;
		Log.i(TAG, requestUrl);

		AsyncHttpServiceHelper.post(requestUrl, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						// TODO Auto-generated method stub
						super.onSuccess(statusCode, headers, response);
						// openkey 开门数字密码，只有在开门失败的情况下才可能存在.只有出现2306才会设置此值
						try {
							Log.i(TAG, response.toString());
							String result = response.get("result").toString();
							String reason = response.get("reason").toString();

							if ("0".equals(result)) {
								if (listener != null)
									listener.onFinish(reason, result);
							} else if ("2306".equals(result)) {
								if (listener != null)
									listener.onFinish(reason, result);
							} else {
								if (listener != null)

									listener.onFailed(reason);
							}
						} catch (Exception e) {
							if (listener != null)
								listener.onFailed(context
										.getResources()
										.getString(R.string.json_analysis_wrong));// JSON
																					// 解析错误
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, responseString,
								throwable);
						if (listener != null)
								listener.onFailed(context
										.getResources()
										.getString(R.string.network_connections_wrong));

					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							Throwable throwable, JSONObject errorResponse) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, throwable,
								errorResponse);
						if (listener != null)
								listener.onFailed(context
										.getResources()
										.getString(R.string.network_connections_wrong));
					}
				});
	}

	/**
	 * 获取门信息
	 * 
	 * @param doorid
	 *            门禁编号
	 * @param listener
	 *            回调函数
	 */
	public void getDoorInfo(String URL, String qrcode,
			final GetOneRecordListener<DoorInfoResp> listener) {

		RequestParams params = new RequestParams();
		params.put("qrcode", qrcode);
		Log.i(TAG, "qrcode = " + qrcode);

		String getParamString = "";

		try {
			getParamString = TokenHelper.FetchToken(context, true);
		} catch (Exception e) {
		}

		String requestUrl = URL + "door/get" + getParamString;
		Log.i(TAG, requestUrl);

		AsyncHttpServiceHelper.post(requestUrl, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						// TODO Auto-generated method stub
						super.onSuccess(statusCode, headers, response);

						try {
							Log.i(TAG, response.toString());
							String result = response.get("result").toString();
							String reason = response.get("reason").toString();
							JSONObject json = response.getJSONObject("door");
							Type type = new TypeToken<DoorInfoResp>() {
							}.getType();

							DoorInfoResp doorInfoResp = gson.fromJson(
									json.toString(), type);

							if (result.equalsIgnoreCase("0")) {
								if (listener != null)
									listener.onFinish(doorInfoResp);
							} else {
								if (listener != null)

									listener.onFailed(reason);
							}
						} catch (Exception e) {
							if (listener != null)
								listener.onFailed(context
										.getResources()
										.getString(R.string.json_analysis_wrong));// JSON
																					// 解析错误
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, responseString,
								throwable);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));

					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							Throwable throwable, JSONObject errorResponse) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, throwable,
								errorResponse);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));
					}
				});
	}

	/**
	 * 是否可授权
	 * 
	 * @param listener
	 *            回调函数
	 */
	public void isGranted(final GetOneRecordListener<String> listener) {

		RequestParams params = new RequestParams();

		String getParamString = "";

		try {//
			getParamString = TokenHelper.FetchToken(context, true);
		} catch (Exception e) {
		}

		String requestUrl = Constant.AUTHORIZATION_IS_GRANTED + getParamString;
		Log.i(TAG, requestUrl);

		AsyncHttpServiceHelper.post(requestUrl, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						// TODO Auto-generated method stub
						super.onSuccess(statusCode, headers, response);
						Log.i(TAG, response.toString());

						try {
							String result = response.get("result").toString();
							String reason = response.get("reason").toString();
							String isgranted = response.get("isgranted")
									.toString();

							if (result.equalsIgnoreCase("0")) {
								if (listener != null)
									listener.onFinish(isgranted);
							} else {
								if (listener != null)

									listener.onFailed(reason);
							}
						} catch (Exception e) {
							if (listener != null)
								listener.onFailed(context
										.getResources()
										.getString(R.string.json_analysis_wrong));// JSON
																					// 解析错误
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, responseString,
								throwable);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));

					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							Throwable throwable, JSONObject errorResponse) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, throwable,
								errorResponse);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));
					}
				});
	}

	/**
	 * 申请
	 * 
	 * @param cid
	 *            授权人编号
	 * @param memo
	 *            申请备注
	 * @param listener
	 *            回调函数
	 */
	public void reapply(String cid, String memo,
			final GetOneRecordListener<String> listener) {

		RequestParams params = new RequestParams();
		params.put("cid", cid);
		params.put("memo", memo);
		Log.i(TAG, "cid = " + cid + ",memo = " + memo);

		String getParamString = "";

		try {//
			getParamString = TokenHelper.FetchToken(context, true);
		} catch (Exception e) {
		}

		String requestUrl = Constant.APPLY_REAPPLY + getParamString;
		Log.i(TAG, requestUrl);

		AsyncHttpServiceHelper.post(requestUrl, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						// TODO Auto-generated method stub
						super.onSuccess(statusCode, headers, response);
						Log.i(TAG, response.toString());

						try {
							String result = response.get("result").toString();
							String reason = response.get("reason").toString();

							if (result.equalsIgnoreCase("0")) {
								if (listener != null)
									listener.onFinish(reason);
							} else {
								if (listener != null)

									listener.onFailed(reason);
							}
						} catch (Exception e) {
							if (listener != null)
								listener.onFailed(context
										.getResources()
										.getString(R.string.json_analysis_wrong));// JSON
																					// 解析错误
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, responseString,
								throwable);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));

					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							Throwable throwable, JSONObject errorResponse) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, throwable,
								errorResponse);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));
					}
				});
	}

	/**
	 * 申请
	 * 
	 * @param account
	 *            授权人手机号码
	 * @param memo
	 *            申请备注
	 * @param listener
	 *            回调函数
	 */
	public void apply(String account, String memo,
			final GetOneRecordListener<String> listener) {

		RequestParams params = new RequestParams();
		params.put("account", account);
		params.put("memo", memo);
		Log.i(TAG, "account = " + account + ",memo = " + memo);

		String getParamString = "";

		try {//
			getParamString = TokenHelper.FetchToken(context, true);
		} catch (Exception e) {
		}

		String requestUrl = Constant.APPLY_APPLY1 + getParamString;
		Log.i(TAG, requestUrl);

		AsyncHttpServiceHelper.post(requestUrl, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						// TODO Auto-generated method stub
						super.onSuccess(statusCode, headers, response);
						Log.i(TAG, response.toString());

						try {
							String result = response.get("result").toString();
							String reason = response.get("reason").toString();

							if (result.equalsIgnoreCase("0")) {
								if (listener != null)
									listener.onFinish(reason);
							} else {
								if (listener != null)

									listener.onFailed(reason);
							}
						} catch (Exception e) {
							if (listener != null)
								listener.onFailed(context
										.getResources()
										.getString(R.string.json_analysis_wrong));// JSON
																					// 解析错误
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, responseString,
								throwable);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));

					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							Throwable throwable, JSONObject errorResponse) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, throwable,
								errorResponse);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));
					}
				});
	}

	/**
	 * 授权（主动授权）
	 * 
	 * @param mobile
	 *            目标用户手机号
	 * @param bid
	 *            小区编号
	 * @param usertype
	 *            授权类型，1家人，2朋友，3访客，4送货
	 * @param granttype
	 *            二次授权，0没有，1有
	 * @param autype
	 *            授权类型，0临时，1限时，2永久
	 * @param starttime
	 *            授权生效起始时间
	 * @param stoptime
	 *            授权生效结束时间
	 * @param memo
	 *            备注，拒绝时为申请备注，批准时为授权备注
	 * @param listener
	 *            回调函数
	 */
	public void authorizeNew(String mobile, String bid, String usertype,
			String granttype, String autype, String starttime, String stoptime,
			String memo, final GetOneRecordListener<String> listener) {

		RequestParams params = new RequestParams();
		params.put("mobile", mobile);
		params.put("bid", bid);
		params.put("usertype", usertype);
		params.put("autype", autype);
		params.put("granttype", granttype);
		params.put("starttime", starttime);
		params.put("stoptime", stoptime);
		params.put("memo", memo);
		Log.i(TAG, "mobile = " + mobile + ", bid = " + bid + ", usertype = "
				+ usertype + ", granttype = " + granttype + " ,autype = "
				+ autype + " starttime = " + starttime + ", stoptime = "
				+ stoptime + ", memo = " + memo);

		String getParamString = "";

		try {//
			getParamString = TokenHelper.FetchToken(context, true);
		} catch (Exception e) {
		}

		String requestUrl = Constant.AUTHORIZATION_AUTHORIZE + getParamString;
		Log.i(TAG, requestUrl);

		AsyncHttpServiceHelper.post(requestUrl, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						// TODO Auto-generated method stub
						super.onSuccess(statusCode, headers, response);

						try {
							Log.i(TAG, response.toString());
							String result = response.get("result").toString();
							String reason = response.get("reason").toString();

							if (result.equalsIgnoreCase("0")) {
								if (listener != null)
									listener.onFinish(reason);
							} else {
								if (listener != null)

									listener.onFailed(reason);
							}
						} catch (Exception e) {
							if (listener != null)
								listener.onFailed(context
										.getResources()
										.getString(R.string.json_analysis_wrong));// JSON
																					// 解析错误
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, responseString,
								throwable);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));

					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							Throwable throwable, JSONObject errorResponse) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, throwable,
								errorResponse);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));
					}
				});
	}

	/**
	 * 再次授权（主动授权）
	 * 
	 * @param toid
	 *            目标用户编号
	 * @param bid
	 *            小区编号
	 * @param autype
	 *            授权类型，0临时，1限时，2永久
	 * @param usertype
	 *            授权类型，1家人，2朋友，3访客，4送货
	 * @param granttype
	 *            二次授权，0没有，1有
	 * @param starttime
	 *            授权生效起始时间
	 * @param stoptime
	 *            授权生效结束时间
	 * @param memo
	 *            备注，拒绝时为申请备注，批准时为授权备注
	 * @param listener
	 *            回调函数
	 */
	public void reAuthorize(String toid, String bid, String usertype,
			String granttype, String autype, String starttime, String stoptime,
			String memo, final GetOneRecordListener<String> listener) {

		RequestParams params = new RequestParams();
		params.put("toid", toid);
		params.put("bid", bid);
		params.put("usertype", usertype);
		params.put("autype", autype);
		params.put("granttype", granttype);
		params.put("starttime", starttime);
		params.put("stoptime", stoptime);
		params.put("memo", memo);
		Log.i(TAG, "toid = " + toid + ", bid = " + bid + ", usertype = "
				+ usertype + ", granttype = " + granttype + " ,autype = "
				+ autype + " starttime = " + starttime + ", stoptime = "
				+ stoptime + ", memo = " + memo);
		String getParamString = "";

		try {//
			getParamString = TokenHelper.FetchToken(context, true);
		} catch (Exception e) {
		}

		String requestUrl = Constant.AUTHORIZATION_RE_AUTHORIZE
				+ getParamString;
		Log.i(TAG, requestUrl);

		AsyncHttpServiceHelper.post(requestUrl, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						// TODO Auto-generated method stub
						super.onSuccess(statusCode, headers, response);

						try {
							Log.i(TAG, response.toString());
							String result = response.get("result").toString();
							String reason = response.get("reason").toString();

							if (result.equalsIgnoreCase("0")) {
								if (listener != null)
									listener.onFinish(reason);
							} else {
								if (listener != null)

									listener.onFailed(reason);
							}
						} catch (Exception e) {
							if (listener != null)
								listener.onFailed(context
										.getResources()
										.getString(R.string.json_analysis_wrong));// JSON
																					// 解析错误
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, responseString,
								throwable);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));

					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							Throwable throwable, JSONObject errorResponse) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, throwable,
								errorResponse);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));
					}
				});
	}

	/**
	 * 取消授权
	 * 
	 * @param id
	 *            授权编号
	 * 
	 * @param listener
	 *            回调函数
	 */
	public void cancelAuthorize(String id,
			final GetOneRecordListener<String> listener) {

		RequestParams params = new RequestParams();
		params.put("id", id);
		Log.i(TAG, "id = " + id);

		String getParamString = "";

		try {//
			getParamString = TokenHelper.FetchToken(context, true);
		} catch (Exception e) {
		}

		String requestUrl = Constant.AUTHORIZATION_CANCEL + getParamString;
		Log.i(TAG, requestUrl);

		AsyncHttpServiceHelper.post(requestUrl, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						// TODO Auto-generated method stub
						super.onSuccess(statusCode, headers, response);

						try {
							Log.i(TAG, response.toString());
							String result = response.get("result").toString();
							String reason = response.get("reason").toString();

							if (result.equalsIgnoreCase("0")) {
								if (listener != null)
									listener.onFinish(reason);
							} else {
								if (listener != null)

									listener.onFailed(reason);
							}
						} catch (Exception e) {
							if (listener != null)
								listener.onFailed(context
										.getResources()
										.getString(R.string.json_analysis_wrong));// JSON
																					// 解析错误
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, responseString,
								throwable);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));

					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							Throwable throwable, JSONObject errorResponse) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, throwable,
								errorResponse);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));
					}
				});
	}

	/**
	 * 批复申请（被动授权）
	 * 
	 * @param applyid
	 *            申请编号
	 * @param bid
	 *            小区编号
	 * @param approve
	 *            批准结果，1批准，2拒绝
	 * @param usertype
	 *            授权类型，1家人，2朋友，3访客，4送货
	 * @param autype
	 *            授权类型，0临时，1限时，2永久
	 * @param granttype
	 *            二次授权，0没有，1有
	 * @param starttime
	 *            授权生效起始时间
	 * @param stoptime
	 *            授权生效结束时间
	 * @param memo
	 *            备注，拒绝时为申请备注，批准时为授权备注
	 * @param listener
	 *            回调函数
	 */
	public void approveNew(String applyid, String bid, String approve,
			String usertype, String autype, String granttype, String starttime,
			String stoptime, String memo,
			final GetOneRecordListener<String> listener) {

		RequestParams params = new RequestParams();
		params.put("applyid", applyid);
		params.put("bid", bid);
		params.put("approve", approve);
		params.put("usertype", usertype);
		params.put("autype", autype);
		params.put("granttype", granttype);
		params.put("starttime", starttime);
		params.put("stoptime", stoptime);
		params.put("memo", memo);
		Log.i(TAG, "applyid = " + applyid + ", bid = " + bid + ", approve = "
				+ approve + ", usertype = " + usertype + ", autype = " + autype
				+ ", granttype = " + granttype + ", starttime = " + starttime
				+ ", stoptime = " + stoptime + ", memo = " + memo);

		String getParamString = "";

		try {//
			getParamString = TokenHelper.FetchToken(context, true);
		} catch (Exception e) {
		}

		String requestUrl = Constant.APPLY_APPROVE + getParamString;
		Log.i(TAG, requestUrl);

		AsyncHttpServiceHelper.post(requestUrl, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						// TODO Auto-generated method stub
						super.onSuccess(statusCode, headers, response);
						try {
							Log.i(TAG, response.toString());
							String result = response.get("result").toString();
							String reason = response.get("reason").toString();

							if (result.equalsIgnoreCase("0")) {
								if (listener != null)
									listener.onFinish(reason);
							} else {
								if (listener != null)

									listener.onFailed(reason);
							}
						} catch (Exception e) {
							if (listener != null)
								listener.onFailed(context
										.getResources()
										.getString(R.string.json_analysis_wrong));// JSON
																					// 解析错误
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, responseString,
								throwable);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));

					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							Throwable throwable, JSONObject errorResponse) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, throwable,
								errorResponse);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));
					}
				});
	}

	/**
	 * 批复申请（被动授权）
	 * 
	 * @param applyid
	 *            申请编号
	 * @param approve
	 *            批准结果，1批准，2拒绝
	 * @param autype
	 *            授权类型，0临时，1限时，2永久
	 * @param granttype
	 *            二次授权，0没有，1有
	 * @param starttime
	 *            授权生效起始时间
	 * @param stoptime
	 *            授权生效结束时间
	 * @param memo
	 *            备注，拒绝时为申请备注，批准时为授权备注
	 * @param listener
	 *            回调函数
	 */
	public void approve(String applyid, String approve, String autype,
			String granttype, String starttime, String stoptime, String memo,
			final GetOneRecordListener<String> listener) {

		RequestParams params = new RequestParams();
		params.put("applyid", applyid);
		params.put("approve", approve);
		params.put("autype", autype);
		params.put("granttype", granttype);
		params.put("starttime", starttime);
		params.put("stoptime", stoptime);
		params.put("memo", memo);
		Log.i(TAG, "applyid = " + applyid + ", approve = " + approve
				+ ", autype = " + autype + ", granttype = " + granttype
				+ ", starttime = " + starttime + ", stoptime = " + stoptime
				+ ", memo = " + memo);

		String getParamString = "";

		try {//
			getParamString = TokenHelper.FetchToken(context, true);
		} catch (Exception e) {
		}

		String requestUrl = Constant.APPLY_APPROVE + getParamString;
		Log.i(TAG, requestUrl);

		AsyncHttpServiceHelper.post(requestUrl, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						// TODO Auto-generated method stub
						super.onSuccess(statusCode, headers, response);
						try {
							Log.i(TAG, response.toString());
							String result = response.get("result").toString();
							String reason = response.get("reason").toString();

							if (result.equalsIgnoreCase("0")) {
								if (listener != null)
									listener.onFinish(reason);
							} else {
								if (listener != null)

									listener.onFailed(reason);
							}
						} catch (Exception e) {
							if (listener != null)
								listener.onFailed(context
										.getResources()
										.getString(R.string.json_analysis_wrong));// JSON
																					// 解析错误
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, responseString,
								throwable);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));

					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							Throwable throwable, JSONObject errorResponse) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, throwable,
								errorResponse);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));
					}
				});
	}

	/**
	 * 获取申请历史
	 * 
	 * @param lasttime
	 *            最后一条申请时间
	 * @param limit
	 *            默认10
	 * @param listener
	 *            返回接口
	 */
	public void getApplyHistoryList(
			final GetMutilResultListener<AuthorizationListResp> listener) {

		RequestParams params = new RequestParams();

		String getParamString = "";

		try {
			getParamString = TokenHelper.FetchToken(context, true);
		} catch (Exception e) {
		}

		String requestUrl = Constant.APPLY_HISTORY_LIST + getParamString;

		Log.i(TAG, requestUrl);

		AsyncHttpServiceHelper.post(requestUrl, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						// TODO Auto-generated method stub
						super.onSuccess(statusCode, headers, response);
						try {
							Log.i(TAG, response.toString());
							String result = response.get("result").toString();
							String reason = response.get("reason").toString();

							if (result.equalsIgnoreCase("0")) {
								JSONArray applyJson = response
										.getJSONArray("list");
								Type applyType = new TypeToken<List<AuthorizationListResp>>() {
								}.getType();
								List<AuthorizationListResp> applys = gson
										.fromJson(applyJson.toString(),
												applyType);

								if (applys != null) {
									if (listener != null)
										listener.onFinish(applys, "");
								} else {
									if (listener != null)

										listener.onFailed(reason);
								}
							} else {

								if (listener != null)

									listener.onFailed(reason);
							}

						} catch (Exception e) {
							if (listener != null)
								listener.onFailed(context
										.getResources()
										.getString(R.string.json_analysis_wrong));// JSON
																					// 解析错误
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, responseString,
								throwable);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));

					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							Throwable throwable, JSONObject errorResponse) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, throwable,
								errorResponse);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));
					}
				});
	}

	/**
	 * 授权列表
	 * 
	 * @param lasttime
	 *            最后一条申请时间
	 * @param limit
	 *            默认10
	 * @param listener
	 *            返回接口
	 */
	public void getAuthorizationList(
			final GetMutilResultListener<AuthorizationListResp> listener) {

		RequestParams params = new RequestParams();

		String getParamString = "";

		try {
			getParamString = TokenHelper.FetchToken(context, true);
		} catch (Exception e) {
		}

		String requestUrl = Constant.AUTHORIZATION_LIST + getParamString;

		Log.i(TAG, requestUrl);

		AsyncHttpServiceHelper.post(requestUrl, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						// TODO Auto-generated method stub
						super.onSuccess(statusCode, headers, response);
						try {
							Log.i(TAG, response.toString());
							String result = response.get("result").toString();
							String reason = response.get("reason").toString();

							if (result.equalsIgnoreCase("0")) {
								JSONArray applyJson = response
										.getJSONArray("list");
								Type applyType = new TypeToken<List<AuthorizationListResp>>() {
								}.getType();
								List<AuthorizationListResp> auths = gson
										.fromJson(applyJson.toString(),
												applyType);

								if (auths != null) {
									if (listener != null)
										listener.onFinish(auths, "");
								} else {
									if (listener != null)

										listener.onFailed(reason);
								}
							} else {
								if (listener != null)

									listener.onFailed(reason);
							}

						} catch (Exception e) {
							if (listener != null)
								listener.onFailed(context
										.getResources()
										.getString(R.string.json_analysis_wrong));// JSON
																					// 解析错误
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, responseString,
								throwable);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));

					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							Throwable throwable, JSONObject errorResponse) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, throwable,
								errorResponse);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));
					}
				});

	}

	/**
	 * 申请记录列表
	 * 
	 * @param bid
	 *            被授权小区编号
	 * @param toid
	 *            被授权用户编号
	 * @param listener
	 *            返回接口
	 */
	public void getAppForHistoryList(String fromid,
			final GetMutilResultListener<AuthorizationHistoryListResp> listener) {

		RequestParams params = new RequestParams();
		params.put("fromid", fromid);

		String getParamString = "";

		try {
			getParamString = TokenHelper.FetchToken(context, true);
		} catch (Exception e) {
		}

		String requestUrl = Constant.APPLYFOR_HISTORY_LIST + getParamString;

		Log.i(TAG, requestUrl);

		AsyncHttpServiceHelper.post(requestUrl, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						// TODO Auto-generated method stub
						super.onSuccess(statusCode, headers, response);
						try {
							Log.i(TAG, response.toString());
							String result = response.get("result").toString();
							String reason = response.get("reason").toString();

							if (result.equalsIgnoreCase("0")) {
								JSONArray applyJson = response
										.getJSONArray("list");
								Type applyType = new TypeToken<List<AuthorizationHistoryListResp>>() {
								}.getType();
								List<AuthorizationHistoryListResp> auths = gson
										.fromJson(applyJson.toString(),
												applyType);

								if (auths != null) {
									if (listener != null)
										listener.onFinish(auths, "");
								} else {
									if (listener != null)

										listener.onFailed(reason);
								}
							} else {
								if (listener != null)

									listener.onFailed(reason);
							}

						} catch (Exception e) {
							if (listener != null)
								listener.onFailed(context
										.getResources()
										.getString(R.string.json_analysis_wrong));// JSON
																					// 解析错误
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, responseString,
								throwable);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));

					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							Throwable throwable, JSONObject errorResponse) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, throwable,
								errorResponse);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));
					}
				});

	}

	/**
	 * 授权记录列表
	 * 
	 * @param bid
	 *            被授权小区编号
	 * @param toid
	 *            被授权用户编号
	 * @param listener
	 *            返回接口
	 */
	public void getAuthorizationHistoryList(String bid, String toid,
			String fromid,
			final GetMutilResultListener<AuthorizationHistoryListResp> listener) {

		RequestParams params = new RequestParams();
		params.put("bid", bid);
		params.put("toid", toid);
		params.put("fromid", fromid);
		Log.i(TAG, "bid = " + bid + " , toid = " + toid);
		String getParamString = "";

		try {
			getParamString = TokenHelper.FetchToken(context, true);
		} catch (Exception e) {
		}

		String requestUrl = Constant.AUTHORIZATION_HISTORY_LIST
				+ getParamString;

		Log.i(TAG, requestUrl);

		AsyncHttpServiceHelper.post(requestUrl, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						// TODO Auto-generated method stub
						super.onSuccess(statusCode, headers, response);
						try {
							Log.i(TAG, response.toString());
							String result = response.get("result").toString();
							String reason = response.get("reason").toString();

							if (result.equalsIgnoreCase("0")) {
								JSONArray applyJson = response
										.getJSONArray("list");
								Type applyType = new TypeToken<List<AuthorizationHistoryListResp>>() {
								}.getType();
								List<AuthorizationHistoryListResp> auths = gson
										.fromJson(applyJson.toString(),
												applyType);

								if (auths != null) {
									if (listener != null)
										listener.onFinish(auths, "");
								} else {
									if (listener != null)

										listener.onFailed(reason);
								}
							} else {
								if (listener != null)

									listener.onFailed(reason);
							}

						} catch (Exception e) {
							if (listener != null)
								listener.onFailed(context
										.getResources()
										.getString(R.string.json_analysis_wrong));// JSON
																					// 解析错误
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, responseString,
								throwable);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));

					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							Throwable throwable, JSONObject errorResponse) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, throwable,
								errorResponse);
						if (listener != null)
							listener.onFailed(context
									.getResources()
									.getString(R.string.network_connections_wrong));
					}
				});

	}
}
